package com.jie.langlangmall.service.impl;

import cn.hutool.core.util.StrUtil;
import com.jie.langlangmall.dao.PmsSkuStockDao;
import com.jie.langlangmall.mapper.PmsSkuStockMapper;
import com.jie.langlangmall.model.PmsSkuStock;
import com.jie.langlangmall.model.PmsSkuStockExample;
import com.jie.langlangmall.service.PmsSkuStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author Jie
 * @Date 2024/11/24
 * @Email 3217998214@qq.com
 * @Description sku商品库存管理
 */
@Service
public class PmsSkuStockServiceImpl implements PmsSkuStockService {

    @Autowired
    private PmsSkuStockMapper skuStockMapper;
    @Autowired
    private PmsSkuStockDao skuStockDao;

    /**
     * 根据商品ID及sku编码模糊搜索sku库存
     *
     * @param pid
     * @param keyword
     * @return
     */
    @Override
    public List<PmsSkuStock> list(Long pid, String keyword) {
        PmsSkuStockExample example = new PmsSkuStockExample();
        PmsSkuStockExample.Criteria criteria = example.createCriteria().andProductIdEqualTo(pid);
        if (!StrUtil.isEmpty(keyword)) {
            criteria.andSkuCodeLike("%" + keyword + "%");
        }
        return skuStockMapper.selectByExample(example);
    }

    /**
     * 更新库存信息
     *
     * @param pid
     * @param skuStockList
     * @return
     */
    @Override
    public void update(Long pid, List<PmsSkuStock> skuStockList) {
        List<PmsSkuStock> filterSkuList = skuStockList.stream()
                .filter(item -> pid.equals(item.getProductId()))
                .collect(Collectors.toList());
        skuStockDao.replaceList(filterSkuList);
    }


}
